//
//  Extra1ViewController.m
//  ToucheTouche
//
//  Created by polytech polytech on 11/01/12.
//  Copyright (c) 2012 polytech. All rights reserved.
//

#import "Extra1ViewController.h"
#import "GameAventureViewController.h"
#import "Level.h"

@implementation Extra1ViewController

@synthesize button10,button11,button12,button13,button14,button15,button16,button17,button18,button19,button20,button21,button22,button23,button24,button25,button26,button27,button28,button29,button30,button31,button32,button33,button34,button35,button36,button37,button38,button39,button40,button41,button42,button43,button44,button45,button46,button47,button48,button49,button50,button51,button52,button53,button54,button55,button56,button57,button58,button59;
@synthesize imageView10,imageView11, imageView12,imageView13,imageView14,imageView15,imageView16,imageView17,imageView18,imageView19,imageView20, imageView21,imageView22,imageView23,imageView24,imageView25,imageView26,imageView27,imageView28,imageView29,imageView30,imageView31,imageView32,imageView33,imageView34,imageView35,imageView36,imageView37,imageView38,imageView39,imageView40,imageView41,imageView42,imageView43,imageView44,imageView45,imageView46,imageView47,imageView48,imageView49,imageView50,imageView51,imageView52,imageView53,imageView54,imageView55,imageView56,imageView57,imageView58,imageView59;
@synthesize allImagesView;
@synthesize labelTmp;
@synthesize labelScore;
@synthesize allButtons;
@synthesize score;
@synthesize temps, nbImages;
@synthesize gameAventureViewController;
@synthesize delegate;
@synthesize floatGameScore;
@synthesize intTimerLevel;
@synthesize alertLevel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}



- (IBAction)actionImage:(id)sender{
    UIButton *buttonTouched = sender;
    for(NSInteger index=0;index < [allButtons count]; index++) {
        if([allButtons objectAtIndex:index]==buttonTouched){
            UIImageView *img = [allImagesView objectAtIndex:index];
            if(img.image !=nil){
                img.image =nil;
                score+=5;
                nbImages+=1;
                [[Level sharedLevel] initExtraScore:score];
                labelScore.text = [NSString stringWithFormat:@"%.0f", [[Level sharedLevel] floatExtraScore]];
            }
         }
    }
}


/* methode timer */
- (void)timer:(NSInteger) tmp{
    temps = tmp;
    chrono = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES] retain];
    // Affiche le compteur
    labelTmp.text = [NSString stringWithFormat:@"%d", temps];
}

/* methode affichant le chrono */
- (void) updateTimer: (NSTimer *) aTimer{
    --temps;
    
    // Affiche l'état du compte à rebour    
    labelTmp.text = [NSString stringWithFormat:@"%d", temps];

    // Arrète le NSTimer lorsque le compte à rebour est à zéro
    if ( temps <= 0 ) {
        [self stopTimer];
    }
}

/* methode arret timer */
- (void)stopTimer {
    [chrono invalidate];
    chrono = nil;
    NSInteger life=0;
    if(nbImages>=35){
        if([[Level sharedLevel] floatLife] <=90){
            [[Level sharedLevel] addLife:10 :2];
            life=10;
        }else if([[Level sharedLevel] floatLife] <=95){
            [[Level sharedLevel] addLife:5 :1];
            life=5;
        }
    }
    else if(nbImages >=30){
        if([[Level sharedLevel] floatLife] <=95){
            [[Level sharedLevel] addLife:5 :1];
            life=5;
        }
    }
    // Affiche l'alerte avec le score
    alertLevel = [[UIAlertView alloc] initWithTitle:@"Résultat de l'extra" message:[NSString stringWithFormat:@"Score : %.0f \nVie gagnée : %d\nPassage au niveau 2", [[Level sharedLevel] floatExtraScore],life] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertLevel show];
    [alertLevel release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  {
    if (alertView == alertLevel) {
        if (buttonIndex == 0) {
            
            // ajout du score de l'extra au score global
            [self.gameAventureViewController addScoreNextExtra];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"AddScoreNextExtraNotification" object:nil];
            
            // mise a jour du nouveau niveau [bonnes valeurs]
            // init : score, score to add, score for extra, life, life to remove, lifeprogress, lifeprogress to remove, time level, time level to remove, current level, time image, background, image
            //[[Level sharedLevel] initLevel:[[Level sharedLevel] floatScore] :10 :250 :[[Level sharedLevel] floatLife] :5 :[[Level sharedLevel] floatLifeProgress] :0.05 :36 :1 :2 :0.8: @"backgroundLevel2": @"imageLevel2"];
            
            // POUR LES TESTS
            [[Level sharedLevel] initLevel:[[Level sharedLevel] floatScore] :10 :20 :[[Level sharedLevel] floatLife] :5 :[[Level sharedLevel] floatLifeProgress] :0.05 :10 :1 :2 :0.8: @"": @"imageTest.png"];
            
            

            NSLog(@"Score actuel : %.0f", [[Level sharedLevel] floatScore]);
            NSLog(@"Score a ajouter : %.0f", [[Level sharedLevel] floatScoreToAdd]);
            NSLog(@"vie actuelle : %.0f", [[Level sharedLevel] floatLife]);
            NSLog(@"vie a enlever : %.0f", [[Level sharedLevel] floatLifeToRemove]);
            NSLog(@"lifeprogress : %.0f", [[Level sharedLevel] floatLifeProgress]);
            NSLog(@"lifeprogress to remove : %.2f", [[Level sharedLevel] floatLifeProgressToRemove]);
            NSLog(@"temps du niveau : %d", [[Level sharedLevel] intTimeLevel]);
            NSLog(@"numero niveau : %d", [[Level sharedLevel] intCurrentLevel]);
            
            
            [self.gameAventureViewController initLabel]; // init des labels
            [[NSNotificationCenter defaultCenter] postNotificationName:@"InitLabelNotification" object:nil];
            [self.gameAventureViewController timerImage]; // deplacement image
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TimerNotification" object:nil];
            [self.gameAventureViewController timerLevel:[[Level sharedLevel] intTimeLevel]]; // temps du niveau
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TimerLevelNotification" object:nil];
            
            
            
            
            // Redirection vers le jeu
            [self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [self dismissModalViewControllerAnimated:YES];
            
        }
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    allImagesView = [[NSArray alloc] initWithObjects:imageView10,imageView11, imageView12,imageView13,imageView14,imageView15,imageView16,imageView17,imageView18,imageView19,imageView20, imageView21,imageView22,imageView23,imageView24,imageView25,imageView26,imageView27,imageView28,imageView29,imageView30,imageView31,imageView32,imageView33,imageView34,imageView35,imageView36,imageView37,imageView38,imageView39,imageView40,imageView41,imageView42,imageView43,imageView44,imageView45,imageView46,imageView47,imageView48,imageView49,imageView50,imageView51,imageView52,imageView53,imageView54,imageView55,imageView56,imageView57,imageView58,imageView59, nil];
    
    allButtons = [[NSArray alloc] initWithObjects:button10,button11,button12,button13,button14,button15,button16,button17,button18,button19,button20,button21,button22,button23,button24,button25,button26,button27,button28,button29,button30,button31,button32,button33,button34,button35,button36,button37,button38,button39,button40,button41,button42,button43,button44,button45,button46,button47,button48,button49,button50,button51,button52,button53,button54,button55,button56,button57,button58,button59, nil];
    
    
    NSString *fileName = [NSString stringWithFormat: @"imageTest2.png"];
    for (UIImageView *imageview in allImagesView) {
        imageview.image = [UIImage imageNamed:fileName];
    }
    score = 0;
    nbImages=0;
    [[Level sharedLevel] initExtraScore:score];
    labelScore.text = [NSString stringWithFormat:@"%.0f", score];
    [self timer:7];
    
    
    
    

    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setLabelScore:nil];
    [self setLabelTmp:nil];
    
    for (UIButton *buttons in allButtons) {
        buttons = nil;
    }
    
    for (UIImageView *imageview in allImagesView) {
        imageview = nil;
    }
    self.temps=0;
    self.nbImages=0;
    
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
       return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void)dealloc {
    [labelScore release];
    [labelTmp release];
    
    for (UIButton *buttons in allButtons) {
        [buttons release];
    }
    
    for (UIImageView *imageview in allImagesView) {
        [imageview release];
    }
    [allButtons release];
    [allImagesView release];
    
    [super dealloc];
}
@end
